<template>
  <div class="login_continer" :span="24">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginform"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到Brioni后台</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginform.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginform.password"
              type="password"
              :prefix-icon="Lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              :loading="loading"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import type { loginInDto } from 'dto/login/loginDto.ts'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/publicFun.ts'

//收集账号密码的数据
let loginform: loginInDto = reactive({ username: 'admin', password: '123456' })
//user仓库
let userStore = useUserStore()
//路由
let $router = useRouter()
let $route = useRoute()
//是否加载
let loading = ref(false)

//自定义校验账号的规则
const validateUserName = (_rule: any, value: any, callback: any) => {
  //rule：即为校验规则对象
  //value：即为表单元素文本内容
  //函数:如果符合条件callBack放行通过即为
  //如果不符合条件callBack方法，注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback('账号长度至少三位')
  }
}
//自定义校验密码的规则
const validatePassword = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback('密码长度6~18')
  }
}

//校验规则配置
//required必须验证，message错误信息，trigger触发方式：blur为没有聚焦，change为正在输入触发
let rules = reactive({
  username: [
    // { required: true, message: '账号不可为空', trigger: 'blur' },
    // { min: 3, max: 20, message: '账号长度为6~20', trigger: 'change' },
    //自定义校验规则
    { trigger: 'change', validator: validateUserName },
  ],
  password: [
    // { required: true, message: '账号长度为6~18', trigger: 'blur',min:6,max:18 }
    //自定义校验规则
    { trigger: 'change', validator: validatePassword },
  ],
})
//获取el-form组件
let loginForms = ref()

/**
 * 登录
 */
const login = async () => {
  //校验表单规则后不满足规则便中断函数，满足则继续执行
  await loginForms.value.validate()
  loading.value = true
  try {
    await userStore.userLogin(loginform)
    setTimeout(() => {
      //获取其他页面传回的路由参数进行重定向
      let redirect: any = $route.query.redirect
      console.log('114', redirect)
      $router.push({ path: redirect || '/' })
    }, 2000)

    ElNotification({
      type: 'success',
      message: '登录成功',
      title: 'Hi ' + getTime() + '好',
      duration: 2000,
    })
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
</script>

<script lang="ts">
export default {
  name: 'Login',
}
</script>

<style scoped lang="scss">
.login_continer {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 20px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
